class AccountController < ApplicationController
  model   :user
  
  def decide_to_login_or_logout
    if session['login_out'] == 'Login' 
      redirect_to(:action => "login")
    else
      redirect_to(:action => "logout")
    end
  end

  def login
    case @request.method
      when :post
      if @session['user'] = User.authenticate(@params['user_login'], @params['user_password'])
        flash['notice'] = "Login successful"
        if @requestUrl = @session['requestUrl']
          session['login_out'] = "Log Out"
          redirect_to(@requestUrl)
        else
          redirect_back_or_default :action => "welcome"
        end
      else
        @login    = @params['user_login']
        @message  = "Login unsuccessful"
      end
    else
      @session['requestUrl'] = @request.env['HTTP_REFERER']  
    end
  end
  
  def signup
    case @request.method
      when :post
      @user = User.new(@params['user'])
      
      if @user.save      
        @session['user'] = User.authenticate(@user.login, @params['user']['password'])
        flash['notice']  = "Signup successful"
        redirect_back_or_default :action => "welcome"          
      end
      when :get
      @user = User.new
    end      
  end
  
  def sponsor_signup
    case @request.method
      when :post
      @user = User.new(@params['user'])
      
      if @user.save      
        @session['user'] = User.authenticate(@user.login, @params['user']['password'])
        flash['notice']  = "Signup successful"
        redirect_back_or_default :action => "welcome"          
      end
      when :get
      @user = User.new
    end      
  end
  
  def delete
    if @params['id'] and @session['user']
      @user = User.find(@params['id'])
      @user.destroy
    end
    redirect_back_or_default :action => "welcome"
  end  
  
  def logout
    @session['login_out']="Login"
    @session['user'] = nil
  end
  
  def welcome
  end
  
end
